import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import type { AddItem, SysDictItem, SysDictItemUpdate, TableListItem } from './data';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';

type Pagination = { page?: number; pageSize?: number };

export async function queryAllSysDictValue(
  params: Pagination & {},
): Promise<RequestData<TableListItem>> {
  const { page = 1, pageSize = 20 } = params;
  const res: ResponsePaginationData<SysDictItem> = await request('/sys/dict/', {
    params: { page, pageSize },
  });
  return {
    success: true,
    data: res.data.list,
  };
}

export async function getSysDict(params: { id: string }): Promise<ResponseData<SysDictItem>> {
  return request(`/sys/dict/code/${params.id}`);
}

export async function addSysDict(params: AddItem): Promise<ResponseData<SysDictItem>> {
  return request(`/sys/dict/code`, { method: 'POST', data: params });
}

/**
 * 修改规则
 * @param params
 */
export async function updateSysDict(params: SysDictItemUpdate): Promise<ResponseData<SysDictItem>> {
  const { id } = params;
  return request(`/sys/dict/keys/${id}`, { data: params, method: 'POST' });
}
